# gg pie chart
# from STHDA, 2018/12/1
library(ggplot2)

df <- data.frame(
    group = c("Male", "Female", "Child"),
    value = c(25, 25, 50)
)

bp <- ggplot(df, aes(x = "", y = value, fill = group)) +
    geom_bar(width = 1, stat = "identity")
bp

pie <- bp + coord_polar("y", start = 0)
pie

# custom palette
pie + scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9"))

pie + scale_fill_brewer(palette = "Dark2")

pie + scale_fill_brewer(palette = "Blues") +
    theme_minimal()

pie + scale_fill_grey() +
    theme_minimal()

# create pie from factor variable
head(PlantGrowth)

ggplot(PlantGrowth, aes(x = factor(1), fill = group)) +
    geom_bar(width = 1) +
    coord_polar("y")


blank_theme <- theme_minimal() +
    theme(
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.ticks = element_blank(),
        plot.title = element_text(size = 14, face = "bold"))

# Apply blank theme
library(scales)
pie + scale_fill_grey() +  blank_theme +
    theme(axis.text.x = element_blank()) +
    geom_text(aes(y = value / 3 + c(0, cumsum(value)[-length(value)]),
                  label = percent(value / 100)), size = 5)

# Use brewer palette
pie + scale_fill_brewer("Blues") + blank_theme +
    theme(axis.text.x = element_blank()) +
    geom_text(aes(y = value / 3 + c(0, cumsum(value)[-length(value)]),
                  label = percent(value / 100)), size = 5)
